/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJFrame.java
 *
 * Created on Sep 26, 2009, 1:23:35 AM
 */
package gpa4;

import gpa4.*;
import javax.swing.JDialog;
import java.awt.event.KeyEvent;
import java.awt.KeyboardFocusManager;
import java.awt.KeyEventDispatcher;

/**
 *
 * @author LoH_Zhang_M
 */
/*
 * Main frame which the game is generated and scores shown
 */
public class NewJFrame extends javax.swing.JFrame {

    /** Creates new form NewJFrame */
    public NewJFrame(String args[]) {

        initComponents();
        filename = "pieces.bin";
        ked = new KeyEventDispatcher() {

            public boolean dispatchKeyEvent(KeyEvent e) {
                if (e.getID() == KeyEvent.KEY_PRESSED) {
                    dp.dispatchKeyPress(e);
                }
                return false;
            }
        };
        try {
            if (args[0].equals("-f") && (args[1].isEmpty() == false)) {
                filename = args[1];
            }
        } catch (ArrayIndexOutOfBoundsException e) {
        }
        npp1 = new NextPiecePanel(jPanel2.getWidth(), jPanel2.getHeight());
        pf1 = new PlayField(jPanel1.getWidth(), jPanel1.getHeight(),0, filename);
        npp2 = new NextPiecePanel(jPanel3.getWidth(), jPanel3.getHeight());
        pf2 = new PlayField(jPanel4.getWidth(), jPanel4.getHeight(),1, filename);
        dp = new DialogPanel();
        jPanel1.add(pf1);
        jPanel2.add(npp1);
        jPanel4.add(pf2);
        jPanel3.add(npp2);
        lScore1.setText("0");
        us = new UpdateScoreThread(this);
        lFour1.setText("4 Pieces: " + pf1.getSquare4());
        lFive1.setText("5 Pieces: " + pf1.getSquare5());
        lSix1.setText("6 Pieces: " + pf1.getSquare6());
        lSeven1.setText("4 Pieces: " + pf1.getSquare4());
        lLine1.setText("Lines cleared: " + pf1.getLinesRemoved());
        lScore1.setText("Score: " + pf1.getScore());
        lSquare1.setText("Squares: " + pf1.getSquareTotal());
        lPiece1.setText("Pieces: " + pf1.getPieceCount());
        lFour2.setText("4 Pieces: " + pf2.getSquare4());
        lFive2.setText("5 Pieces: " + pf2.getSquare5());
        lSix2.setText("6 Pieces: " + pf2.getSquare6());
        lSeven2.setText("4 Pieces: " + pf2.getSquare4());
        lLine2.setText("Lines cleared: " + pf2.getLinesRemoved());
        lScore2.setText("Score: " + pf2.getScore());
        lSquare2.setText("Squares: " + pf2.getSquareTotal());
        lPiece2.setText("Pieces: " + pf2.getPieceCount());
        jLabel5.setText("Welcome");
        gc = new GameController(pf1, pf2, npp1, npp2,dp, filename);
        gc.setupNewGame(GameState.Welcome);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        lLine1 = new javax.swing.JLabel();
        lScore1 = new javax.swing.JLabel();
        lFour1 = new javax.swing.JLabel();
        lFive1 = new javax.swing.JLabel();
        lSeven1 = new javax.swing.JLabel();
        lSix1 = new javax.swing.JLabel();
        lSquare1 = new javax.swing.JLabel();
        lPiece1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        lSix2 = new javax.swing.JLabel();
        lFour2 = new javax.swing.JLabel();
        lScore2 = new javax.swing.JLabel();
        lLine2 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lSeven2 = new javax.swing.JLabel();
        lFive2 = new javax.swing.JLabel();
        lSquare2 = new javax.swing.JLabel();
        lPiece2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jMenuBar = new javax.swing.JMenuBar();
        jMenuFile = new javax.swing.JMenu();
        jMenuItemPlayPause = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        jMenuItemQuit = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItemSingle = new javax.swing.JMenuItem();
        jMenuItemDemo = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        jMenuItemComp = new javax.swing.JMenuItem();
        jMenuItemLoc = new javax.swing.JMenuItem();
        jMenuItemInter = new javax.swing.JMenuItem();
        jMenuItemQual = new javax.swing.JMenuItem();
        jMenuEdit = new javax.swing.JMenu();
        jMenuItemConfig = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(254, 254, 254));
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setPreferredSize(new java.awt.Dimension(240, 360));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 240, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 360, Short.MAX_VALUE)
        );

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 130, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 81, Short.MAX_VALUE)
        );

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Next Piece");

        lLine1.setFont(new java.awt.Font("Tahoma", 0, 14));
        lLine1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lLine1.setText("lLine1");

        lScore1.setFont(new java.awt.Font("Tahoma", 0, 18));
        lScore1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lScore1.setText("lScore1");

        lFour1.setText("lFour1");

        lFive1.setText("LFive");

        lSeven1.setText("lSeven1");

        lSix1.setText("lSix1");

        lSquare1.setText("lSquare1");

        lPiece1.setFont(new java.awt.Font("Tahoma", 0, 14));
        lPiece1.setText("lPiece1");

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 130, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 81, Short.MAX_VALUE)
        );

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));
        jPanel4.setPreferredSize(new java.awt.Dimension(240, 360));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 240, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 360, Short.MAX_VALUE)
        );

        lSix2.setText("lSix1");

        lFour2.setText("lFour1");

        lScore2.setFont(new java.awt.Font("Tahoma", 0, 18));
        lScore2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lScore2.setText("lScore1");

        lLine2.setFont(new java.awt.Font("Tahoma", 0, 14));
        lLine2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lLine2.setText("lLine1");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Next Piece");

        lSeven2.setText("lSeven1");

        lFive2.setText("LFive");

        lSquare2.setText("lSquare1");

        lPiece2.setFont(new java.awt.Font("Tahoma", 0, 14));
        lPiece2.setText("lPiece1");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel3.setText("Player 1");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel4.setText("Player 2");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel5.setText("Gameover?");

        jMenuFile.setText("File");

        jMenuItemPlayPause.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemPlayPause.setText("Pause");
        jMenuItemPlayPause.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                playPauseActionPerformed(evt);
            }
        });
        jMenuFile.add(jMenuItemPlayPause);
        jMenuFile.add(jSeparator2);

        jMenuItemQuit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemQuit.setText("Quit");
        jMenuItemQuit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitActionPerformed(evt);
            }
        });
        jMenuFile.add(jMenuItemQuit);

        jMenuBar.add(jMenuFile);

        jMenu2.setText("New Game");

        jMenuItemSingle.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.ALT_MASK));
        jMenuItemSingle.setText("Single Player");
        jMenuItemSingle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemSingleActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItemSingle);

        jMenuItemDemo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.ALT_MASK));
        jMenuItemDemo.setText("Demo Mode");
        jMenuItemDemo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemDemoActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItemDemo);

        jMenu1.setText("Two Player");

        jMenuItemComp.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.ALT_MASK));
        jMenuItemComp.setText("vs Computer");
        jMenuItemComp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemCompActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemComp);

        jMenuItemLoc.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, java.awt.event.InputEvent.ALT_MASK));
        jMenuItemLoc.setText("vs Local");
        jMenuItemLoc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemLocActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemLoc);

        jMenuItemInter.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.ALT_MASK));
        jMenuItemInter.setText("vs Internet");
        jMenuItemInter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemInterActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemInter);

        jMenuItemQual.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.ALT_MASK));
        jMenuItemQual.setText("vs Qualifier");
        jMenuItemQual.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemQualActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemQual);

        jMenu2.add(jMenu1);

        jMenuBar.add(jMenu2);

        jMenuEdit.setText("Edit");

        jMenuItemConfig.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemConfig.setText("Configure Keys");
        jMenuItemConfig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                configActionPerformed(evt);
            }
        });
        jMenuEdit.add(jMenuItemConfig);

        jMenuBar.add(jMenuEdit);

        setJMenuBar(jMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(10, 10, 10)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING)))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(lScore1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(lLine1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(lPiece1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(lFour1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(lSix1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(lSquare1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(lFive1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(lSeven1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(8, 8, 8)
                                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(66, 66, 66)
                        .addComponent(jLabel3)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jLabel2))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(lScore2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lLine2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lPiece2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lFour2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lSix2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lSquare2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lFive2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lSeven2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(131, 131, 131)
                        .addComponent(jLabel4)))
                .addContainerGap(91, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(12, 12, 12)
                            .addComponent(lScore1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(lLine1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lPiece1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lFour1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lFive1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lSeven1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lSix1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lSquare1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(lScore2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(lLine2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lPiece2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lFour2, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lFive2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lSeven2, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lSix2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lSquare2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        if (gc != null){
            gc.keyPress(evt);
        }
    }//GEN-LAST:event_formKeyPressed

    private void quitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quitActionPerformed
        System.exit(0);
    }//GEN-LAST:event_quitActionPerformed

    private void configActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_configActionPerformed
        JDialog jDialogConfig = new JDialog(this, "Configure Keys", true);
        jDialogConfig.getContentPane().add(dp);
        jDialogConfig.pack();
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(ked);
        jDialogConfig.setLocationRelativeTo(null);
        jDialogConfig.setVisible(true);
        KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(ked);
    }//GEN-LAST:event_configActionPerformed

    private void playPauseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_playPauseActionPerformed
        if (gc != null) {
            if (gc.getPaused()) {
                // Game was paused - continue game play
                gc.setPaused(false);
                jMenuItemPlayPause.setText("Pause");
            } else {
                // Game was in play mode - pause game
                gc.setPaused(true);
                if (us.isAlive()==false){
                    us.start();
                }
                jMenuItemPlayPause.setText("Play");
            }
        }
    }//GEN-LAST:event_playPauseActionPerformed

    private void jMenuItemCompActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemCompActionPerformed
        //player vs ai mode
        gc.setPaused(false);
        jMenuItemPlayPause.setText("Pause");
        gc.setupNewGame(GameState.VersusComputer);

        if (gc.isAlive()==false){
            gc.start();
        }
        if (us.isAlive()==false){
            us.start();
        }
    }//GEN-LAST:event_jMenuItemCompActionPerformed

    private void jMenuItemLocActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemLocActionPerformed
        //pvp mode
        gc.setPaused(false);
        jMenuItemPlayPause.setText("Pause");
        gc.setupNewGame(GameState.VersusPlayer);
        if (gc.isAlive()==false){
            gc.start();
        }
        if (us.isAlive()==false){
            us.start();
        }
    }//GEN-LAST:event_jMenuItemLocActionPerformed

    private void jMenuItemInterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemInterActionPerformed
        //player vs network mode
        gc.setPaused(false);
        jMenuItemPlayPause.setText("Pause");
        gc.setupNewGame(GameState.VersusNetwork);
        if (gc.isAlive()==false){
            gc.start();
        }
        if (us.isAlive()==false){
            us.start();
        }
    }//GEN-LAST:event_jMenuItemInterActionPerformed

    private void jMenuItemQualActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemQualActionPerformed
        //ai sent over network (single player)
        gc.setPaused(false);
        jMenuItemPlayPause.setText("Pause");
        gc.setupNewGame(GameState.Qualifier);
        if (gc.isAlive()==false){
            gc.start();
        }
        if (us.isAlive()==false){
            us.start();
        }
    }//GEN-LAST:event_jMenuItemQualActionPerformed

    private void jMenuItemSingleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSingleActionPerformed
        gc.setPaused(false);
        jMenuItemPlayPause.setText("Pause");
        gc.setupNewGame(GameState.Single);
        if (gc.isAlive()==false){
            gc.start();
        }
        if (us.isAlive()==false){
            us.start();
        }
    }//GEN-LAST:event_jMenuItemSingleActionPerformed

    private void jMenuItemDemoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemDemoActionPerformed
        gc.setPaused(false);
        jMenuItemPlayPause.setText("Pause");
        gc.setupNewGame(GameState.DemoMode);
        if (gc.isAlive()==false){
            gc.start();
        }
        if (us.isAlive()==false){
            us.start();
        }
    }//GEN-LAST:event_jMenuItemDemoActionPerformed
    public void updateScores() {
        if (gc != null) {
            lFour1.setText("4 Pieces: " + pf1.getSquare4());
            lFive1.setText("5 Pieces: " + pf1.getSquare5());
            lSix1.setText("6 Pieces: " + pf1.getSquare6());
            lSeven1.setText("4 Pieces: " + pf1.getSquare4());
            lLine1.setText("Lines cleared: " + pf1.getLinesRemoved());
            lScore1.setText("Score: " + pf1.getScore());
            lSquare1.setText("Squares: " + pf1.getSquareTotal());
            lPiece1.setText("Pieces: " + pf1.getPieceCount());
            lFour2.setText("4 Pieces: " + pf2.getSquare4());
            lFive2.setText("5 Pieces: " + pf2.getSquare5());
            lSix2.setText("6 Pieces: " + pf2.getSquare6());
            lSeven2.setText("4 Pieces: " + pf2.getSquare4());
            lLine2.setText("Lines cleared: " + pf2.getLinesRemoved());
            lScore2.setText("Score: " + pf2.getScore());
            lSquare2.setText("Squares: " + pf2.getSquareTotal());
            lPiece2.setText("Pieces: " + pf2.getPieceCount());
            if (gc.getGameover()!=-1){
                if (gc.getGameover()==2){
                    jLabel3.setText("Player 1: Gameover");
                    jLabel4.setText("Player 2: Gameover");
                    if (gc.getWinner()){
                        jLabel5.setText("Player 1 WON!");
                    }else jLabel5.setText("Player 1 LOSES!");
                    
                }else if(gc.getGameover()==1){
                    jLabel3.setText("Player 1");
                    jLabel4.setText("Player 2: Gameover");
                }else if(gc.getGameover()==2){
                    jLabel3.setText("Player 1: Gamover");
                    jLabel4.setText("Player 2");
                }else jLabel5.setText("Playing");
                

            }else{
                jLabel3.setText("Player 1");
                jLabel4.setText("Player 2");
                if (gc.getPaused())
                    jLabel5.setText("Paused");
                else
                    jLabel5.setText("Playing");
            }
        }
        if (gc.getGameType()==GameState.Single || gc.getGameType()==GameState.Qualifier){
            jLabel4.setText("Player 2: Unused");

        }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(final String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new NewJFrame(args).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar;
    private javax.swing.JMenu jMenuEdit;
    private javax.swing.JMenu jMenuFile;
    private javax.swing.JMenuItem jMenuItemComp;
    private javax.swing.JMenuItem jMenuItemConfig;
    private javax.swing.JMenuItem jMenuItemDemo;
    private javax.swing.JMenuItem jMenuItemInter;
    private javax.swing.JMenuItem jMenuItemLoc;
    private javax.swing.JMenuItem jMenuItemPlayPause;
    private javax.swing.JMenuItem jMenuItemQual;
    private javax.swing.JMenuItem jMenuItemQuit;
    private javax.swing.JMenuItem jMenuItemSingle;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JSeparator jSeparator2;
    public javax.swing.JLabel lFive1;
    public javax.swing.JLabel lFive2;
    public javax.swing.JLabel lFour1;
    public javax.swing.JLabel lFour2;
    public javax.swing.JLabel lLine1;
    public javax.swing.JLabel lLine2;
    public javax.swing.JLabel lPiece1;
    public javax.swing.JLabel lPiece2;
    public javax.swing.JLabel lScore1;
    public javax.swing.JLabel lScore2;
    public javax.swing.JLabel lSeven1;
    public javax.swing.JLabel lSeven2;
    public javax.swing.JLabel lSix1;
    public javax.swing.JLabel lSix2;
    public javax.swing.JLabel lSquare1;
    public javax.swing.JLabel lSquare2;
    // End of variables declaration//GEN-END:variables
    PlayField pf1;
    PlayField pf2;
    NextPiecePanel npp1;
    NextPiecePanel npp2;
    DialogPanel dp;
    KeyEventDispatcher ked;
    GameController gc;
    String filename;
    UpdateScoreThread us;
    int gameType;
}

class UpdateScoreThread extends Thread {

    public UpdateScoreThread(NewJFrame frame) {
        njf = frame;
    }

    @Override
    public void run() {
        int rtn = 0;
        while (true) {
            try {
                sleep(stdDelay);
                if (njf.gc.getPaused()==false) njf.updateScores();
                //System.out.println("run");
            } catch (InterruptedException ie) {
            }
        }
    }
    PlayField uptPanel;
    NextPiecePanel nxtPsPanel;
    private boolean isPaused;
    javax.swing.JLabel scoreBoard;
    javax.swing.JLabel lLine;
    javax.swing.JLabel lPiece;
    javax.swing.JLabel square7;
    javax.swing.JLabel square6;
    javax.swing.JLabel square5;
    javax.swing.JLabel square4;
    javax.swing.JLabel numSquare;
    private final int stdDelay = 500;
    public String message;
    private NewJFrame njf;
}
